package com.example.jdbc.druid;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.example.util.DataSourceUtils;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

/**
 * @program: sms
 * @description:
 * @author: zhanghz001
 * @create: 2020-11-09 16:26
 **/
public class DruidDemo01 {
    private DataSource dataSource = null;

    // @Before
    public void before() throws Exception {
        Properties prop = new Properties();
        InputStream is = DruidDemo01.class.getClassLoader().getResourceAsStream("druid.properties");
        prop.load(is);

        //
        dataSource = DruidDataSourceFactory.createDataSource(prop);

        is.close();
    }

    @Test
    public void test01() throws Exception {
        before();
        Connection connection = dataSource.getConnection();
        String sql = "select * from student ";
        PreparedStatement ps = connection.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            int sid = rs.getInt("sid");
            String name = rs.getString("NAME");
            int age = rs.getInt("age");
            Date birthday = rs.getDate("birthday");
            System.out.println(sid + "\t" + name + "\t" + age + "\t" + birthday);
        }
        ps.close();
        connection.close();
        rs.close();
    }

    @Test
    public void test02() throws Exception {

        Connection connection = DataSourceUtils.getConnection();
        String sql = "select * from student ";
        PreparedStatement ps = connection.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            int sid = rs.getInt("sid");
            String name = rs.getString("NAME");
            int age = rs.getInt("age");
            Date birthday = rs.getDate("birthday");
            System.out.println(sid + "\t" + name + "\t" + age + "\t" + birthday);
        }
        ps.close();
        connection.close();
        rs.close();
    }

    @Test
    public void test03() {

    }

    @Test
    public void test04() {

    }

    @Test
    public void test05() {

    }
}
